<?php

namespace app\home\controller;


use think\facade\App;
use think\facade\Request;

class Url extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 我的链接
     */
    public function index() {
        $params['startDate'] = input('startDate', '', 'trim');
        $params['endDate']   = input('endDate', '', 'trim');
        $params['groupId']   = input('groupId', 0);
        $params['channelId'] = input('channelId', 0);
        $params['keyword']   = input('keyword', '', 'trim');
        $params['link']      = input('link', '', 'trim');
        $params['sort']      = input('sort', 'create_time');
        $params['pageSize']  = input('pageSize', 5);
    
        $logic = App::model('Url', 'logic');
        $data = $logic->index($params, $this->userId);

        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 创建短链接
     */
    public function build() {
        $logic = model('Url', 'logic');
        $data = $logic->build($this->userId);

        $this->assign($data);
        return view();
    }

    /**
     * @desc 添加分组
     */
    public function addGroup() {
        $params = [
            'id'          => Request::post('id', 0),
            'action'      => Request::post('action', 'add'),
            'channelName' => Request::post('channelName'),
            'channelCode' => Request::post('channelCode'),
            'status'      => Request::post('status'),
            'remark'      => Request::post('remark'),
        ];

        $logic = model('Url', 'logic');
        $data = [];
        try {
            $data = $logic->addGroup($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }

    /**
     * @desc 设置短链接
     */
    public function setting() {
        $params['id']        = input('id', 0);
        
        $logic = App::model('Url', 'logic');
        $data = $logic->setting($params, $this->userId);
        
        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 创建链接
     */
    public function operateAjax() {
        $params['id']           = Request::post('id', 0);
        $params['action']       = Request::post('action', 'add');
        $params['groupId']      = Request::post('groupId');
        $params['channelId']    = Request::post('channelId');
        $params['paramsSwitch'] = Request::post('paramsSwitch');
        $params['link']         = Request::post('link');
        $params['linkType']     = Request::post('linkType');
        $params['expireTime']   = Request::post('expireTime');
        $params['password']     = Request::post('password');
        $params['keyword']      = Request::post('keyword');
        $params['remark']       = Request::post('remark');
        
        $logic = model('Url', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
    
    /**
     * @desc 设置短链接
     */
    public function statistics() {
        $params['id']        = input('id', 0);
        $params['startDate'] = input('startDate', '', 'trim');
        $params['endDate']   = input('endDate', '', 'trim');
        $params['pageSize']  = input('pageSize', 5);
        
        $logic = App::model('Url', 'logic');
        $data = $logic->statistics($params, $this->userId);

        $this->assign($data);
        
        return view();
    }
}
